package cn.fz.mpdemo.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class UploadUtils {
    // https://blog.csdn.net/weixin_44206275/article/details/139232885
    public static Map<String,String> upload(MultipartFile file, String saveDir) throws IOException {
        // todo 文件验证，后缀，大小
        File dirFile = new File(saveDir);
        dirFile.mkdirs();
        // 获取文件的名称
        String orgFileName = file.getOriginalFilename();
        String suffix = orgFileName.substring(orgFileName.lastIndexOf("."));
        // 通过hutool工具包的IdUtil类获取uuid做为前缀
        String flag = IdUtil.fastSimpleUUID();
        String filename = flag + suffix;
        // String filePath = dirFile.getPath()+filename;
        File file1 = FileUtil.writeBytes(file.getBytes(), new File(dirFile,filename));
        Map<String,String> map = new HashMap<>();
        map.put("orgFileName",orgFileName);
        map.put("suffix",suffix);
        map.put("filename",saveDir+filename);
        return map;
    }
}
